package com.example.edu.Mapper;

import com.example.edu.Entity.Picture;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PictureMapper {
    @Select("select picture_id from picture where md5 = #{md5}")
    Integer getPictureId(@Param("md5") String md5);

    @Insert("insert into picture(md5,type,size) values (#{picture.md5} , #{picture.type} , #{picture.size})")
    @Options(useGeneratedKeys = true, keyProperty = "picture_id") // 自动获取主键
    void addPicture(@Param("picture") Picture picture);

    @Select("select p.picture_id,type,size from user_picture up join picture p on up.picture_id = p.picture_id where up.user_telephone = #{telephone} order by p.picture_id limit #{count} offset #{start}")
    List<Picture> getPictures(@Param("start") int start,@Param("count") int count,@Param("telephone") String telephone);

    @Select("select md5,type from picture where picture_id = #{picture_id}")
    Picture getType(@Param("picture_id") int picture_id);

    @Delete("delete from picture where md5 = #{md5}")
    void deleteByMD5(@Param("md5") String md5);

    @Select("select p.picture_id,p.md5,p.type from picture p left join user_picture up on p.picture_id = up.picture_id " +
            "where up.user_telephone is null ")
    List<Picture> getWildPicture();

    void deleteByMD5s(@Param("md5s") List<String> md5s);

    @Select("select p.picture_id,p.type from article_picture ap join picture p on ap.picture_id = p.picture_id where ap.article_id = #{article_id}")
    List<Picture> getArticlePictures(@Param("article_id") int article_id);
}
